# @Time: 2020/10/07 11:13
# @Author: may
# @Desc:
import time

from data_entry.certificate import Certificate
from selenium.webdriver.common.by import By
from logger import Logger


class DemolitionCompensationAgreementAndRoster(Certificate):
    """拆迁补偿协议及花名册"""

    logger = Logger('DemolitionCompensationAgreementAndRoster').get_log()

    def click_roster(self):
        """点击拆补及花名册菜单"""

        self.click_data_entry()
        self.select_credentials(4)

    def input_info(self, date, name, id_, area_of_house, compensation_standard_type, compensation_standard,
                   total_compensation, compensation_type, payment_date, is_replacement=True, remote_replacement=None):
        """输入拆补各项数据"""

        # 协议签订日期
        self.send_keys((By.NAME, 'xyqdrq'), date)
        # 被补偿方名称
        self.send_keys((By.NAME, 'bbcfmc'), name)
        # 被补偿方身份证号
        self.send_keys((By.NAME, 'bbcfsfzh'), id_)
        # 房屋面积
        self.send_keys((By.NAME, 'yfwmj'), area_of_house)
        # 补偿标准类型
        loc_standard = (By.XPATH, '//div[5]//div[1]//div[1]//div[1]//input[1]')
        self.click(loc_standard)
        self.click((By.XPATH, f'//div[5]/div[1]/div[1]/dl[1]/dd[{compensation_standard_type}]'))
        compensation_standard_type = self.get_attribute(loc_standard)
        # 补偿标准
        self.send_keys((By.NAME, 'bcbz'), compensation_standard)
        # 总补偿款
        self.send_keys((By.NAME, 'zbcjk'), total_compensation)
        # 补偿方式
        loc_type = (By.XPATH, '//div[8]//div[1]//div[1]//div[1]//input[1]')
        self.click(loc_type)
        self.click((By.XPATH, f'//body//dd[{compensation_type}]'))
        compensation_type = self.get_attribute(loc_type)
        # 补偿货币发放日期
        self.send_keys((By.NAME, 'bchbffrq'), payment_date)
        # 原地置换新房屋
        if is_replacement:
            loc_replacement = (By.NAME, 'ydzhxfwfh')
            self.click(loc_replacement)
            time.sleep(1)
            # 进入选择房屋页面
            iframe = self.find_element((By.XPATH, '(//iframe)[last()]'))
            self.switch_to_default_content()
            self.switch_to_frame(iframe)
            self.click((By.XPATH, '//tr[1]//td[1]//div[1]//div[1]//i[1]'))
            self.click((By.LINK_TEXT, '保 存'))
            time.sleep(1)
            self.click((By.LINK_TEXT, '确认'))
            replacement = self.get_attribute(loc_replacement)[:-1]
            data = [date, name, id_, area_of_house, compensation_standard_type, compensation_standard,
                    total_compensation, compensation_type, payment_date, replacement]
        else:
            data = [date, name, id_, area_of_house, compensation_standard_type, compensation_standard,
                    total_compensation, compensation_type, payment_date]
        # 异地置换新房屋
        self.send_keys((By.NAME, 'yidizhfw'), remote_replacement)
        data.append(remote_replacement)
        # 保存
        self.click((By.XPATH, "//a[contains(@class,'saveBlue rightHandel')]"))
        return data

    def add(self, date, name, id_, area_of_house, compensation_standard_type, compensation_standard,
            total_compensation, compensation_type, payment_date, remote_replacement):
        """添加拆补"""
        self.input_info(date, name, id_, area_of_house, compensation_standard_type, compensation_standard,
                        total_compensation, compensation_type, payment_date, remote_replacement)
        time.sleep(1)
        self.click((By.LINK_TEXT, '添加附件'))
        self.upload_easy()
        db_info = self.get_roster(self.project_id)

    def get_roster(self, project_id):
        """根据项目id获取拆补信息"""

        conn = self.get_conn()
        cursor = conn.cursor()

